﻿namespace QMVnSoft.Controls.ChildGridControls
{
    using QMVnSoft.Controls;
    using System;
    using System.Windows.Forms;

    public class DateEditing : TextDate, IDataGridViewEditingControl
    {
        private DataGridView dataGridView;
        private int rowIndex;
        private bool valueChanged;

        public virtual void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
        {
            this.Font = dataGridViewCellStyle.Font;
            this.BackColor = dataGridViewCellStyle.BackColor;
            this.ForeColor = dataGridViewCellStyle.ForeColor;
        }

        public virtual bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
        {
            switch ((keyData & Keys.KeyCode))
            {
                case Keys.Prior:
                case Keys.Next:
                case Keys.End:
                case Keys.Home:
                case Keys.Left:
                case Keys.Up:
                case Keys.Right:
                case Keys.Down:
                    return true;
            }
            return false;
        }

        public virtual object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
        {
            return this.EditingControlFormattedValue;
        }

        private void OnCellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            e.Cancel = false;
        }

        protected override void OnTextChanged(EventArgs e)
        {
            this.valueChanged = true;
            if (this.EditingControlDataGridView != null)
            {
                this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
                base.OnTextChanged(e);
            }
        }

        public virtual void PrepareEditingControlForEdit(bool selectAll)
        {
            this.Font = this.EditingControlDataGridView.Font;
        }

        public virtual DataGridView EditingControlDataGridView
        {
            get
            {
                return this.dataGridView;
            }
            set
            {
                this.dataGridView = value;
                this.dataGridView.CellValidating += new DataGridViewCellValidatingEventHandler(this.OnCellValidating);
            }
        }

        public virtual object EditingControlFormattedValue
        {
            get
            {
                if (DBNull.Value.Equals(base.Value))
                {
                    return DBNull.Value;
                }
                DateTime time = (DateTime) base.Value;
                return time.ToShortDateString();
            }
            set
            {
                string s = value as string;
                if (s != null)
                {
                    base.Value = DateTime.Parse(s);
                }
            }
        }

        public virtual int EditingControlRowIndex
        {
            get
            {
                return this.rowIndex;
            }
            set
            {
                this.rowIndex = value;
            }
        }

        public virtual bool EditingControlValueChanged
        {
            get
            {
                return this.valueChanged;
            }
            set
            {
                this.valueChanged = value;
            }
        }

        public virtual Cursor EditingPanelCursor
        {
            get
            {
                return base.Cursor;
            }
        }

        public virtual bool RepositionEditingControlOnValueChange
        {
            get
            {
                return false;
            }
        }
    }
}

